package org.example.erat.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.erat.dto.ExperimentDTO;
import org.example.erat.dto.ResponseDTO;
import org.example.erat.service.ExperimentService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 实验控制器
 */
@RestController
@RequestMapping("/experiments")
@RequiredArgsConstructor
@Slf4j
public class ExperimentController {

    private final ExperimentService experimentService;

    /**
     * 创建实验
     * @param experimentDTO 实验DTO
     * @return 响应
     */
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseDTO<ExperimentDTO> createExperiment(@Valid @RequestBody ExperimentDTO experimentDTO) {
        log.info("创建实验请求: {}", experimentDTO);
        ExperimentDTO createdExperiment = experimentService.createExperiment(experimentDTO);
        return ResponseDTO.success("实验创建成功", createdExperiment);
    }

    /**
     * 更新实验
     * @param id 实验ID
     * @param experimentDTO 实验DTO
     * @return 响应
     */
    @PutMapping("/{id}")
    public ResponseDTO<ExperimentDTO> updateExperiment(@PathVariable Long id, @Valid @RequestBody ExperimentDTO experimentDTO) {
        log.info("更新实验请求: id={}, experiment={}", id, experimentDTO);
        ExperimentDTO updatedExperiment = experimentService.updateExperiment(id, experimentDTO);
        return ResponseDTO.success("实验更新成功", updatedExperiment);
    }

    /**
     * 删除实验
     * @param id 实验ID
     * @return 响应
     */
    @DeleteMapping("/{id}")
    public ResponseDTO<Void> deleteExperiment(@PathVariable Long id) {
        log.info("删除实验请求: id={}", id);
        experimentService.deleteExperiment(id);
        return ResponseDTO.success("实验删除成功", null);
    }

    /**
     * 获取实验
     * @param id 实验ID
     * @return 响应
     */
    @GetMapping("/{id}")
    public ResponseDTO<ExperimentDTO> getExperiment(@PathVariable Long id) {
        log.info("获取实验请求: id={}", id);
        ExperimentDTO experiment = experimentService.getExperiment(id);
        return ResponseDTO.success(experiment);
    }

    /**
     * 获取所有实验
     * @return 响应
     */
    @GetMapping
    public ResponseDTO<List<ExperimentDTO>> getAllExperiments() {
        log.info("获取所有实验请求");
        List<ExperimentDTO> experiments = experimentService.getAllExperiments();
        return ResponseDTO.success(experiments);
    }

    /**
     * 根据课程ID获取实验列表
     * @param courseId 课程ID
     * @return 响应
     */
    @GetMapping("/course/{courseId}")
    public ResponseDTO<List<ExperimentDTO>> getExperimentsByCourse(@PathVariable Long courseId) {
        log.info("获取课程的实验列表请求: courseId={}", courseId);
        List<ExperimentDTO> experiments = experimentService.getExperimentsByCourse(courseId);
        return ResponseDTO.success(experiments);
    }

    /**
     * 批量创建实验
     * @param courseId 课程ID
     * @param count 实验数量
     * @return 响应
     */
    @PostMapping("/batch")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseDTO<List<ExperimentDTO>> batchCreateExperiments(
            @RequestParam Long courseId,
            @RequestParam int count) {
        log.info("批量创建实验请求: courseId={}, count={}", courseId, count);
        
        if (count <= 0 || count > 20) {
            return ResponseDTO.error(400, "实验数量必须在1-20之间");
        }
        
        List<ExperimentDTO> createdExperiments = experimentService.batchCreateExperiments(courseId, count);
        return ResponseDTO.success("成功创建 " + createdExperiments.size() + " 个实验", createdExperiments);
    }
}
